home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / handson / Java / JBAdventure / MainFrame.java < prev    next >
Encoding:
Java Source  |  1998-03-23  |  11.3 KB  |  387 lines

  1. /* -----------------------------------------------------------------------
  2.     A Java Adventure Game framework.
  3.    -----------------------------------------------------------------------    
  4.     Sample Java program for PC Plus magazine.
  5.     Author: Huw Collingbourne
  6.     
  7.     This program is designed with the intention of keeping
  8.     the visual design separate, as much as possible, from the
  9.     detailed implementation of the game itself.
  10.     
  11.     Although tbis is the 'main' file, it contains very little of the
  12.     real code of the game. It is mostly confined to the visual
  13.     interface. The code is mainly auto-generated by Visual Cafe 2.0
  14.     
  15.     My 'hand-written' code can be found at the bottom of this file.
  16.     
  17.   ----------------------------------------------------------------------- */
  18.  
  19. import java.awt.*;
  20. import java.io.*;
  21. import java.util.*;
  22.  
  23. public class MainFrame extends Frame 
  24.                        implements AdventureConstants
  25. {
  26.     
  27.     
  28.     public MainFrame()
  29.     {
  30.         // This code is automatically generated by Visual Cafe when you add
  31.         // components to the visual environment. It instantiates and initializes
  32.         // the components. To modify the code, only use code syntax that matches
  33.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  34.         // parse your Java file into its visual environment.
  35.  
  36.         //{{INIT_CONTROLS
  37.         setLayout(null);
  38.         setVisible(false);
  39.         setSize(insets().left + insets().right + 405,insets().top + insets().bottom + 305);
  40.         openFileDialog1 = new java.awt.FileDialog(this);
  41.         openFileDialog1.setMode(FileDialog.LOAD);
  42.         openFileDialog1.setTitle("Open");
  43.         //$$ openFileDialog1.move(40,277);
  44.         displayBox = new java.awt.TextArea();
  45.         displayBox.setBounds(insets().left + 12,insets().top + 12,384,180);
  46.         displayBox.setFont(new Font("Dialog", Font.PLAIN, 16));
  47.         add(displayBox);
  48.         NBtn = new java.awt.Button();
  49.         NBtn.setActionCommand("button");
  50.         NBtn.setLabel("N");
  51.         NBtn.setBounds(insets().left + 300,insets().top + 204,36,24);
  52.         NBtn.setBackground(new Color(12632256));
  53.         add(NBtn);
  54.         SBtn = new java.awt.Button();
  55.         SBtn.setActionCommand("button");
  56.         SBtn.setLabel("S");
  57.         SBtn.setBounds(insets().left + 300,insets().top + 228,36,24);
  58.         SBtn.setBackground(new Color(12632256));
  59.         add(SBtn);
  60.         WBtn = new java.awt.Button();
  61.         WBtn.setActionCommand("button");
  62.         WBtn.setLabel("W");
  63.         WBtn.setBounds(insets().left + 264,insets().top + 216,36,24);
  64.         WBtn.setBackground(new Color(12632256));
  65.         add(WBtn);
  66.         EBtn = new java.awt.Button();
  67.         EBtn.setActionCommand("button");
  68.         EBtn.setLabel("E");
  69.         EBtn.setBounds(insets().left + 336,insets().top + 216,36,24);
  70.         EBtn.setBackground(new Color(12632256));
  71.         add(EBtn);
  72.         InputTF = new java.awt.TextField();
  73.         InputTF.setBounds(insets().left + 12,insets().top + 192,170,27);
  74.         add(InputTF);
  75.         TakeBtn = new java.awt.Button();
  76.         TakeBtn.setActionCommand("button");
  77.         TakeBtn.setLabel("Take");
  78.         TakeBtn.setBounds(insets().left + 12,insets().top + 228,76,24);
  79.         TakeBtn.setBackground(new Color(12632256));
  80.         add(TakeBtn);
  81.         DropBtn = new java.awt.Button();
  82.         DropBtn.setActionCommand("button");
  83.         DropBtn.setLabel("Drop");
  84.         DropBtn.setBounds(insets().left + 96,insets().top + 228,73,25);
  85.         DropBtn.setBackground(new Color(12632256));
  86.         add(DropBtn);
  87.         LookBtn = new java.awt.Button();
  88.         LookBtn.setActionCommand("button");
  89.         LookBtn.setLabel("Look");
  90.         LookBtn.setBounds(insets().left + 96,insets().top + 264,71,23);
  91.         LookBtn.setBackground(new Color(12632256));
  92.         add(LookBtn);
  93.         setTitle("The Great Java Adventure!");
  94.         //}}
  95.  
  96.         //{{INIT_MENUS
  97.         mainMenuBar = new java.awt.MenuBar();
  98.         menu1 = new java.awt.Menu("File");
  99.         miNew = new java.awt.MenuItem("New");
  100.         menu1.add(miNew);
  101.         miLoad = new java.awt.MenuItem("Load...");
  102.         menu1.add(miLoad);
  103.         miSave = new java.awt.MenuItem("Save");
  104.         menu1.add(miSave);
  105.         miSaveAs = new java.awt.MenuItem("Save As...");
  106.         menu1.add(miSaveAs);
  107.         menu1.addSeparator();
  108.         miExit = new java.awt.MenuItem("Exit");
  109.         menu1.add(miExit);
  110.         mainMenuBar.add(menu1);
  111.         menu3 = new java.awt.Menu("Help");
  112.         mainMenuBar.setHelpMenu(menu3);
  113.         miAbout = new java.awt.MenuItem("About..");
  114.         menu3.add(miAbout);
  115.         mainMenuBar.add(menu3);
  116.         setMenuBar(mainMenuBar);
  117.         //$$ mainMenuBar.move(4,277);
  118.         //}}
  119.  
  120.         //{{REGISTER_LISTENERS
  121.         SymWindow aSymWindow = new SymWindow();
  122.         this.addWindowListener(aSymWindow);
  123.         SymAction lSymAction = new SymAction();
  124.         miLoad.addActionListener(lSymAction);
  125.         miAbout.addActionListener(lSymAction);
  126.         miExit.addActionListener(lSymAction);
  127.         NBtn.addActionListener(lSymAction);
  128.         SBtn.addActionListener(lSymAction);
  129.         WBtn.addActionListener(lSymAction);
  130.         EBtn.addActionListener(lSymAction);
  131.         miSave.addActionListener(lSymAction);
  132.         TakeBtn.addActionListener(lSymAction);
  133.         LookBtn.addActionListener(lSymAction);
  134.         DropBtn.addActionListener(lSymAction);
  135.         SymComponent aSymComponent = new SymComponent();
  136.         LookBtn.addComponentListener(aSymComponent);
  137.         //}}
  138.         updatedisplayBox(""); //!! Show description on starting
  139.  
  140.     }
  141.  
  142.     public MainFrame(String title)
  143.     {
  144.         this();
  145.         setTitle(title);
  146.     }
  147.  
  148.     public synchronized void show()
  149.     {
  150.         move(50, 50);
  151.         super.show();
  152.     }
  153.  
  154.     static public void main(String args[])
  155.     {
  156.         (new MainFrame()).show();
  157.     }
  158.  
  159.     public void addNotify()
  160.     {
  161.         // Record the size of the window prior to calling parents addNotify.
  162.         Dimension d = getSize();
  163.         
  164.         super.addNotify();
  165.  
  166.         if (fComponentsAdjusted)
  167.             return;
  168.  
  169.         // Adjust components according to the insets
  170.         setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
  171.         Component components[] = getComponents();
  172.         for (int i = 0; i < components.length; i++)
  173.         {
  174.             Point p = components[i].getLocation();
  175.             p.translate(insets().left, insets().top);
  176.             components[i].setLocation(p);
  177.         }
  178.         fComponentsAdjusted = true;
  179.     }
  180.  
  181.     // Used for addNotify check.
  182.     boolean fComponentsAdjusted = false;
  183.  
  184.     //{{DECLARE_CONTROLS
  185.     java.awt.FileDialog openFileDialog1;
  186.     java.awt.TextArea displayBox;
  187.     java.awt.Button NBtn;
  188.     java.awt.Button SBtn;
  189.     java.awt.Button WBtn;
  190.     java.awt.Button EBtn;
  191.     java.awt.TextField InputTF;
  192.     java.awt.Button TakeBtn;
  193.     java.awt.Button DropBtn;
  194.     java.awt.Button LookBtn;
  195.     //}}
  196.  
  197.     //{{DECLARE_MENUS
  198.     java.awt.MenuBar mainMenuBar;
  199.     java.awt.Menu menu1;
  200.     java.awt.MenuItem miNew;
  201.     java.awt.MenuItem miLoad;
  202.     java.awt.MenuItem miSave;
  203.     java.awt.MenuItem miSaveAs;
  204.     java.awt.MenuItem miExit;
  205.     java.awt.Menu menu3;
  206.     java.awt.MenuItem miAbout;
  207.     //}}
  208.  
  209.     class SymWindow extends java.awt.event.WindowAdapter
  210.     {
  211.         public void windowClosing(java.awt.event.WindowEvent event)
  212.         {
  213.             Object object = event.getSource();
  214.             if (object == MainFrame.this)
  215.                 Frame1_WindowClosing(event);
  216.         }
  217.     }
  218.  
  219.     void Frame1_WindowClosing(java.awt.event.WindowEvent event)
  220.     {
  221.         hide();         // hide the Frame
  222.         dispose();      // free the system resources
  223.         System.exit(0); // close the application
  224.     }
  225.  
  226.     class SymAction implements java.awt.event.ActionListener
  227.     {
  228.         public void actionPerformed(java.awt.event.ActionEvent event)
  229.         {
  230.             Object object = event.getSource();
  231.             if (object == miLoad)
  232.                 miLoad_Action(event);
  233.             else if (object == miAbout)
  234.                 miAbout_Action(event);
  235.             else if (object == miExit)
  236.                 miExit_Action(event);
  237.             else if (object == NBtn)
  238.                 NBtn_Action(event);
  239.             else if (object == SBtn)
  240.                 SBtn_Action(event);
  241.             else if (object == WBtn)
  242.                 WBtn_Action(event);
  243.             else if (object == EBtn)
  244.                 EBtn_Action(event);
  245.             else if (object == miSave)
  246.                 miSave_Action(event);
  247.             else if (object == TakeBtn)
  248.                 TakeBtn_Action(event);
  249.             else if (object == LookBtn)
  250.                 LookBtn_Action(event);
  251.             else if (object == DropBtn)
  252.                 DropBtn_Action(event);
  253.         }
  254.     }
  255.     
  256.     void miAbout_Action(java.awt.event.ActionEvent event)
  257.     {
  258.         //{{CONNECTION
  259.         // Action from About Create and show as modal
  260.         (new AboutDialog(this, true)).show();
  261.         //}}
  262.     }
  263.  
  264.     void miExit_Action(java.awt.event.ActionEvent event)
  265.     {
  266.         //{{CONNECTION
  267.         // Action from Exit Create and show as modal
  268.         (new QuitDialog(this, true)).show();
  269.         //}}
  270.     }
  271.  
  272.  
  273. // -------------------------------------------------
  274. // ----- MY OWN HAND-WRITTEN CODE FROM HERE ON -----
  275. // -------------------------------------------------
  276.  
  277. // The Implementor class 'wraps' up all the internal details of
  278. // the game itself.
  279.     Implementor Imp = new Implementor();
  280. // The remainder of the code deals with the visual objects
  281. // on the form.
  282.  
  283.     // --- The displayBox textArea
  284.     void updatedisplayBox(String msg) {
  285.         // !!! argument is now a String - any special message returned
  286.         // by Adventure (e.g. "No Exit!\n")
  287.         //
  288.         // if msg <> "" display the message
  289.         // display description of room anyway
  290.         String s = msg;
  291.         if (s.equals("")) // if no special message show room description
  292.         {                 // else show message 
  293.             Room r = Imp.getAdv().getplayer().getroom();
  294.             Vector thingshere = r.getthings();
  295.             Vector inventory = Imp.getAdv().getplayer().getthings();
  296.                 s = "You are in " + 
  297.                 r.getname() + "\n" +
  298.                 r.getdescription() + "\n" +
  299.                 "--- Things here ---\n";
  300.                 for (Enumeration e = thingshere.elements(); e.hasMoreElements(); ) 
  301.                 s = s + ((Thing)e.nextElement()).getname() + ", ";                
  302.                 s = s + "\n--- You have ---\n";
  303.                 for (Enumeration e = inventory.elements(); e.hasMoreElements(); ) 
  304.                 s = s + ((Thing)e.nextElement()).getname() + ", ";
  305.         }
  306.         displayBox.setText( s + "\n" );
  307.     }
  308.  
  309.     // --- The Direction buttons
  310.     // North
  311.     void NBtn_Action(java.awt.event.ActionEvent event)
  312.     {
  313.         updatedisplayBox(Imp.getAdv().movePlayerTo(NORTH));
  314.     }
  315.  
  316.     // South
  317.     void SBtn_Action(java.awt.event.ActionEvent event)
  318.     {        
  319.         updatedisplayBox(Imp.getAdv().movePlayerTo(SOUTH));
  320.     }
  321.  
  322.     // West
  323.     void WBtn_Action(java.awt.event.ActionEvent event)
  324.     {                
  325.         updatedisplayBox(Imp.getAdv().movePlayerTo(WEST));
  326.     }
  327.  
  328.     // East
  329.     void EBtn_Action(java.awt.event.ActionEvent event)
  330.     {        
  331.         updatedisplayBox(Imp.getAdv().movePlayerTo(EAST));
  332.     }
  333.  
  334.     // Save
  335.     void miSave_Action(java.awt.event.ActionEvent event)
  336.     {
  337.         displayBox.setText("Saving...");
  338.         displayBox.setText(Imp.SaveAdv());
  339.     }
  340.  
  341.     // Load    
  342.     void miLoad_Action(java.awt.event.ActionEvent event)
  343.     {
  344.         // openFileDialog1.show(); - may use this later (?)
  345.           displayBox.setText("Loading...");
  346.           displayBox.setText(Imp.LoadAdv());
  347.     }
  348.  
  349.     
  350.  
  351.     void TakeBtn_Action(java.awt.event.ActionEvent event)
  352.     {
  353.         // Take object specified in InputTF
  354.         updatedisplayBox(Imp.getAdv().takeOb(InputTF.getText()));        
  355.     }
  356.  
  357.  
  358.     void DropBtn_Action(java.awt.event.ActionEvent event)
  359.     {
  360.         // Drop object specified in InputTF
  361.         updatedisplayBox(Imp.getAdv().dropOb(InputTF.getText()));        
  362.     }
  363.     
  364.         void LookBtn_Action(java.awt.event.ActionEvent event)
  365.     {
  366.         // Show room description
  367.         updatedisplayBox("");
  368.     }
  369.  
  370.  
  371.     class SymComponent extends java.awt.event.ComponentAdapter
  372.     {
  373.         public void componentHidden(java.awt.event.ComponentEvent event)
  374.         {
  375.             Object object = event.getSource();
  376.             if (object == LookBtn)
  377.                 LookBtn_ComponentHidden(event);
  378.         }
  379.     }
  380.  
  381.     void LookBtn_ComponentHidden(java.awt.event.ComponentEvent event)
  382.     {
  383.         // to do: code goes here.
  384.     }
  385. }
  386.  
  387.